package com.shujia;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Mutation;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
import org.apache.hadoop.hbase.mapreduce.TableMapReduceUtil;
import org.apache.hadoop.hbase.mapreduce.TableMapper;
import org.apache.hadoop.hbase.mapreduce.TableReducer;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;

// 从HBase的stu表中读取数据，统计每个班级的性别人数，最后将数据写回HBase的stu_gender_cnt表
public class Demo06MRReadAndWriteHBase {
    // Map端
    public static class ReadHBase extends TableMapper<Text, IntWritable> {
        @Override
        protected void map(ImmutableBytesWritable key, Result value, Mapper<ImmutableBytesWritable, Result, Text, IntWritable>.Context context) throws IOException, InterruptedException {
            String clazz = Bytes.toString(value.getValue("info".getBytes(), "clazz".getBytes()));
            String gender = Bytes.toString(value.getValue("info".getBytes(), "gender".getBytes()));

            context.write(new Text(clazz + "," + gender), new IntWritable(1));
        }
    }

    // Reduce端
    public static class WriteHBase extends TableReducer<Text, IntWritable, NullWritable> {
        @Override
        protected void reduce(Text key, Iterable<IntWritable> values, Reducer<Text, IntWritable, NullWritable, Mutation>.Context context) throws IOException, InterruptedException {
            int cnt = 0;
            for (IntWritable value : values) {
                cnt += value.get();
            }

            Put put = new Put(key.getBytes());
            put.addColumn("cf1".getBytes(), "cnt".getBytes(), (cnt + "").getBytes());

            context.write(NullWritable.get(), put);
        }
    }

    // Driver端
    public static void main(String[] args) throws IOException, InterruptedException, ClassNotFoundException {
        Configuration conf = HBaseConfiguration.create();
        conf.set("hbase.zookeeper.quorum", "master:2181,node1:2181,node2:2181");

        Job job = Job.getInstance(conf);

        // 对Map任务进行配置
        TableMapReduceUtil.initTableMapperJob(
                TableName.valueOf("stu")
                , new Scan()
                , ReadHBase.class
                , Text.class
                , IntWritable.class
                , job
        );

        // 对Reduce任务进行配置
        TableMapReduceUtil.initTableReducerJob(
                "stu_gender_cnt"
                , WriteHBase.class
                , job
        );


        job.waitForCompletion(true);


    }

    /*
     * create 'stu_gender_cnt','cf1'
     * hadoop jar HBase-1.0-jar-with-dependencies.jar com.shujia.Demo06MRReadAndWriteHBase
     */

}
